'use strict';
/**
 * Created by FreezeSoul on 2015/9/28.
 */
define([
        'angular',
        'app/modules/source/define'
    ],
    function (angular) {
        angular.module('ad.source')
            .factory("adSourceService", ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function getPage(current, size, search) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/search';
                    adRequest(url, {
                        criteria: search, pageIndex: current, pageSize: size
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function getAll() {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/all';
                    adRequest(url, {}).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function addItem(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/add';
                    adRequest(url, {
                        sourceName: row.datasource_name,
                        sourceIp: row.datasource_ip,
                        sourcePort: row.datasource_port,
                        sourceUser: row.datasource_user,
                        sourcePassword: row.datasource_password,
                        sourceDatabase: row.datasource_database,
                        sourceType: row.datasource_type
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function deleteItems(rows) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/delete';
                    var ids = _.map(rows, function (row) {
                        return row.datasource_id;
                    });
                    adRequest(url, {
                        sourceIds: angular.toJson(ids)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function updateItem(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/update';
                    adRequest(url, {
                        sourceId: row.datasource_id,
                        sourceName: row.datasource_name,
                        sourceIp: row.datasource_ip,
                        sourcePort: row.datasource_port,
                        sourceUser: row.datasource_user,
                        sourcePassword: row.datasource_password,
                        sourceDatabase: row.datasource_database,
                        sourceType: row.datasource_type
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function testSource(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/test';
                    adRequest(url, {
                        sourceName: row.datasource_name,
                        sourceIp: row.datasource_ip,
                        sourcePort: row.datasource_port,
                        sourceUser: row.datasource_user,
                        sourcePassword: row.datasource_password,
                        sourceDatabase: row.datasource_database,
                        sourceType: row.datasource_type
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function executeSql(row, sql, params) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/execute';
                    adRequest(url, {
                        sqlStr: sql,
                        sqlParams: angular.toJson(params),
                        sourceIp: row.datasource_ip,
                        sourcePort: row.datasource_port,
                        sourceUser: row.datasource_user,
                        sourcePassword: row.datasource_password,
                        sourceDatabase: row.datasource_database,
                        sourceType: row.datasource_type
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function invokeService(service, funStr, params) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/invoke';
                    adRequest(url, {
                        urlService: service,
                        urlFunction: funStr,
                        urlParams: angular.toJson(params),
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function getMeta(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/source/meta';
                    adRequest(url, {
                        sourceName: row.datasource_name,
                        sourceIp: row.datasource_ip,
                        sourcePort: row.datasource_port,
                        sourceUser: row.datasource_user,
                        sourcePassword: row.datasource_password,
                        sourceDatabase: row.datasource_database,
                        sourceType: row.datasource_type
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                return {
                    getPage: getPage,
                    getAll: getAll,
                    getMeta: getMeta,
                    addItem: addItem,
                    deleteItems: deleteItems,
                    updateItem: updateItem,
                    testSource: testSource,
                    executeSql: executeSql,
                    invokeService: invokeService
                };
            }]);
    });